<template>
	<!-- 个人中心 收货地址 -->
	<view class="Jy_shop_address_page">
		<view class="Jy_shop_content-container">
			<view class="address_content_group">
				
				<view class="Jy_collection_empty_group" v-if="addressList.length == 0">
					<DataEmpty imageUrl="https://louiscloud-1304691295.cos.ap-guangzhou.myqcloud.com/home/%E7%A9%BA%E6%95%B0%E6%8D%AE.png" startPadding="true" tipsText="暂无收货地址"></DataEmpty>
				</view>
				
				<view class="address_content_box" v-else>
					<u-radio-group v-model="radiovalue" placement="column" @change="radioGroupChange">
						<view
							:class="['address-list', manageNotIsShow ? '' : 'address-list-rd']"
							v-for="(item, index) in addressList"
							:key="index"
							:style="{ 'border-radius': addressList.length == 1 ? '10rpx' : '' }"
						>
							<view class="address-container">
								<view class="address_basic_info_area_group">
									<view class="address_image_box">
										<image src="http://120.78.177.187:8083/crmebimage/public/mpImage/address/default.png" mode="aspectFill" v-if="item.isDefault"></image>
										<text v-else>{{item.realName.substring(0,2)}}</text>
									</view>
									<view class="address_info_group">
										<view class="address_person_box">
											<view class="person_name"><text>{{item.realName}}</text></view>
											<view class="person_phone"><text>{{item.phone}}</text></view>
											<view class="address_state" v-if="item.isDefault"><text>默认</text></view>
										</view>
										<view class="detail_address_box"><text>{{item.province}} {{item.city}} {{item.district}} {{item.detail}}</text></view>
									</view>
									<view class="update_group"> 
										<view class="update_box" @click="getUpdateAddress(item)" v-if="switchIsShow"><image src="http://120.78.177.187:8083/crmebimage/public/mpImage/address/exit.png" mode="aspectFill"></image></view>
										<view class="sy_button_box" v-else>
											<text>使用</text>
										</view>
									</view>
								</view>
								<view class="more_button_group" :style="{ display: manageNotIsShow ? 'none' : 'black' }">
									<view class="default_button_box">
										<u-radio :customStyle="{ marginBottom: '8px' }"  :name="item.id" activeColor="#ff7500" @change="radioChange()"></u-radio>
										<text>默认地址</text> 
									</view>
									<view class="detele_button_box" @click="getAddressDel(item.id)"><text>删除</text></view> 
								</view>
							</view>
						</view>
					</u-radio-group>
				</view> 
			</view>
			<view class="Jy_add_area_group" v-if="switchIsShow">
				<view class="left_management_group">
					<view class="mg_button" v-if="manageNotIsShow" @click="manageNotIsShow = false"><text>管理</text></view>
					<view class="exit_mg_button" v-else @click="manageNotIsShow = true"><text>完成</text></view>
				</view>
				<view class="add_address_group">
					<view class="add_address_box" @click="getAddAddress()"><text>添加收货地址</text></view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
// 数据为空
import DataEmpty from '../../../../components/DataEmpty.vue';
	
export default {
	components: {
		DataEmpty
	},
	data() {
		return {
			// 是否显示管理面板
			manageNotIsShow: true,
			addressList: [],
			radiovalue: '',
			switchIsShow: true,
			page: 1,
			limit: 10
		};
	},
	onLoad(option) {
		this.getSwitchTabs(option);
		this.getAddressList()
	},
	methods: {
		radioGroupChange(n) {
		},
		radioChange(n) {
			console.log('radioChange', n);
			this.getDefaultAddress(n);
		},
		// 点击跳转到修改收货地址页面
		getUpdateAddress(item) {
			uni.navigateTo({
				url: '/pagesA/main/customerMyCenter/shippingAddress/addAddress/addAddress?key=1&data=' + JSON.stringify(item)
			})
		},
		// 跳转到新增收货地址页面
		getAddAddress() {
			uni.navigateTo({
				url: '/pagesA/main/customerMyCenter/shippingAddress/addAddress/addAddress?key=2'
			})
		},
		// 切换选择地址
		getSwitchTabs(option) {
			if (option.key == 1) {
				this.switchIsShow = false;
			}
		},
		// 获取地址列表
		getAddressList() {
			uni.showLoading({})
			let data = {
				page:this.page,
				limit:this.limit
			}
			this.sendRequest({
				url: "/api/front/address/list",
				data: data,
				success: res => {
					this.addressList = res.data.data.list;
					this.addressList.map((item, index) => {
						if (item.isDefault) {
							this.radiovalue = item.id.toString();
							uni.hideLoading()
						}
					})
				}
			})
		},
		// 删除收货地址
		getAddressDel(id) {
			uni.showLoading({
				title: '正在删除'
			})
			this.sendRequest({
				url: "/api/front/address/del",
				data: id,
				method: 'post',
				success: res => {
					uni.hideLoading()
					uni.showToast({
						icon:'success',
						title: '删除成功'
					})
					setTimeout(() => {
						uni.redirectTo({
							url: '/pagesA/main/customerMyCenter/shippingAddress/addAddress/addAddress'
						})
					}, 1000)
				}
			})
		},
		// 修改默认地址
		getDefaultAddress(id) {
			uni.showLoading({
				title: '正在设置'
			})
			this.sendRequest({
				url: "/api/front/address/default/set",
				data: id,
				method: 'post',
				success: res => {
					uni.hideLoading()
					uni.showToast({
						icon:'success',
						title: '设置成功'
					})
					setTimeout(() => {
						uni.redirectTo({
							url: '/pagesA/main/customerMyCenter/shippingAddress/addAddress/addAddress'
						})
					}, 1000)
				}
			})
		}
	}
};
</script>

<style lang="scss" scoped>
@import 'shippingAddress.scss';
</style>
